import Vue from 'vue'
import axios from 'axios'
import VueAxios from 'vue-axios'
import NProgress from 'vue-nprogress'
import { sync } from 'vuex-router-sync'
import iView from 'iview';
import './styles/iview.less';       // 使用 CSS
import "./styles/style.scss";
import App from './Root.vue'
import router from './router'
import store from './store'
import * as filters from './filters'
import apiConfig from './api-config'
import { TOGGLE_MENUNAV } from 'vuex-store/mutation-types'
import componentIview from './components/iview/index.js'
import componentStatus from './components/status/index.js'
import CommonComponent from './components/common/index.js'

import {scrollTop} from './utils/dom'

Vue.use(iView);
Vue.use(componentIview);
Vue.use(componentStatus);
Vue.use(CommonComponent);

Vue.router = router;
Vue.use(VueAxios, axios);

Vue.use(NProgress,{
  latencyThreshold: 100, // Number of ms before progressbar starts showing, default: 100,
  router: true, // Show progressbar when navigating routes, default: true
  http: false // Show progressbar when doing Vue.http, default: true
});

// Enable devtools
Vue.config.devtools = apiConfig.Tools;
//message
const iViewMessage = Vue.prototype.$Message;
Vue.prototype.$Message = {
  name: iViewMessage.name,
  info (options) {
    if (options==='') {return false}
    let opt = options;
      if(typeof options == 'string'){
          opt = {content:options,};
          if(options.search('@')==0){
              opt.closable = true;
              opt.duration = 0;
              opt.content = options.replace(/@/,'');
          }
          if(options.search('\n')>=0){
              opt.render= h => {
                  return (<textarea disabled={true} style='vertical-align: middle;background: none;border: none;'>{options}</textarea>)
              };
              delete  opt.content
          }
      }
    return iViewMessage.info(opt);
  },
  success (options) {
    if (options==='') {return false}
      let opt = options;
      if(typeof options == 'string'){
          opt = {content:options,};
          if(options.search('@')==0){
              opt.closable = true;
              opt.duration = 0;
              opt.content = options.replace(/@/,'');
          }
          if(options.search('\n')>=0){
              opt.render= h => {
                  return (<textarea disabled={true} style='vertical-align: middle;background: none;border: none;'>{options}</textarea>)
              };
              delete  opt.content
          }
      }
    return iViewMessage.success(opt);
  },
  warning (options) {
    if (options==='') {return false}
      let opt = options;
      if(typeof options == 'string'){
          opt = {content:options,};
          if(options.search('@')==0){
              opt.closable = true;
              opt.duration = 0;
              opt.content = options.replace(/@/,'');
          }
          if(options.search('\n')>=0){
              opt.render= h => {
                  return (<textarea disabled={true} style='vertical-align: middle;background: none;border: none;'>{options}</textarea>)
              };
              delete  opt.content
          }
      }
    return iViewMessage.warning(opt);
  },
  error (options) {
    if (options==='') {return false}
      let opt = options;
      if(typeof options == 'string'){
          opt = {content:options,};
          if(options.search('@')==0){
              opt.closable = true;
              opt.duration = 0;
              opt.content = options.replace(/@/,'');
          }
          if(options.search('\n')>=0){
              opt.render= h => {
                  return (<textarea disabled={true} style='vertical-align: middle;background: none;border: none;'>{options}</textarea>)
              };
              delete  opt.content
          }
      }
    return iViewMessage.error(opt);
  },
  loading (options) {
    if (options==='') {return false}
    return iViewMessage.loading(options);
  },
  config (options) {
    iViewMessage.config(options);
  },
  destroy () {
    iViewMessage.destroy();
  }
};
iViewMessage.config({
    duration: 2.6
});
//page
let _page = {
  set:function (obj,data) {
    if(!data){console.warn("没有分页信息"); return false}
    if(!data.total){obj.page_total=0;console.warn("没有分页total信息");}else if(obj.page_total!=parseInt(data.total)){obj.page_total = parseInt(data.total)}
    if(!data.size){obj.page_row=0;console.warn("没有分页size信息");}else if(obj.page_row!=parseInt(data.size)){obj.page_row = parseInt(data.size)}
    if(!data.current_page){obj.page_current=0;console.warn("没有分页current_page信息");}else if(obj.page_current!=parseInt(data.current_page)){obj.page_current = parseInt(data.current_page)}
  },
  def:function (obj) {
    obj.page_total=0;
    obj.page_row=20;
    obj.page_current=1;
  },
  setData:function (key,obj) {
    _page._data[key] = obj;
  },
  getData:function (key) {
    const d = _page._data[key];
    delete _page._data[key];
    return  d;
  },
  _data:{}
};
Vue.prototype.$clientPage = _page;
//tabs
let _tabs = {
  setData:function (key,obj) {
    _tabs._data[key] = obj;
  },
  getData:function (key) {
    const d = _tabs._data[key];
    delete _tabs._data[key];
    return  d;
  },
  _data:{}
};
Vue.prototype.$clientTab = _tabs;

sync(store, router);

const nprogress = new NProgress({ parent: '.nprogress-container' });

const { state,_actions,_mutations } = store;

router.beforeEach((route, redirect, next) => {
  /*if (state.app.device.isMobile && state.app.menunav.show) {
    store.commit(TOGGLE_MENUNAV, false)
  }
  */
    if(route.name!='Login'){
        if(!store.getters.app.user.token){
            next({name:'Login'});
            return false
        }else {
            /*console.log(redirect);
            if(store.getters.app.router.length){
                store.getters.app.router.forEach((v,i)=>{
                    if(v.name == route.name){
                        _actions.popRouter[0](i+1);
                    }
                });
                _actions.putRouter[0]({name:route.name,title:route.meta&&route.meta.title,params:route.params,query:route.query});
            }else {
                _actions.putRouter[0]({name:route.name,title:route.meta&&route.meta.title,params:route.params,query:route.query})
            }*/
        }
    }
    scrollTop();
  next()
});

Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
});

const app = new Vue({
  router,
  store,
  nprogress,
  ...App
});

export { app, router, store }
